package com.ambimmort.ucagent.modules.AAAReader;

import com.ambimmort.ucagent.modules.server.codec.UcRawMessage;
import com.ambimmort.umessage.UcType;
//import com.yeecloud.a3pub.AAAMem;
//import com.yeecloud.a3pub.IPv4Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.concurrent.atomic.AtomicLong;

public class A3Pub {

//    private static final Logger LOGGER = LoggerFactory.getLogger(A3Pub.class);
//    private static final int MSG_AAA = 1;
//    private static final int MAX_RETRY = 3;
//    static AtomicLong total = new AtomicLong(0L);
//    static AtomicLong ok = new AtomicLong(0L);
//    static AtomicLong protocalErr = new AtomicLong(0L);
//    static AtomicLong exception = new AtomicLong(0L);
//
//
//    private final byte[] message;
//
//    public A3Pub(byte[] message) {
//        this.message = message;
//    }
//
//    public UcRawMessage process() {
////        A3Pub.LOGGER.info("A3Pub get one AAApac");
//        try {
//            ByteBuffer bbuf = ByteBuffer.wrap(this.message, 0, this.message.length);
//            bbuf.order(ByteOrder.LITTLE_ENDIAN);
//            int msgType = bbuf.get();
//            if (MSG_AAA != msgType) {
//                A3Pub.protocalErr.incrementAndGet();
//                return null;
//            }
//            int length = bbuf.getInt();
//            if (length != this.message.length) {
//                A3Pub.protocalErr.incrementAndGet();
//                return null;
//            }
//
//            int adLen = bbuf.getInt();
//            byte[] adBuf = new byte[adLen];
//            bbuf.get(adBuf);
//
//            int ipRawLen = bbuf.getInt();
//            byte[] ipRawBuf = new byte[ipRawLen];
//            bbuf.get(ipRawBuf);
//
////				String ip = InetAddress.getByAddress(ipRaw).getHostAddress();
//            int onlineInt = bbuf.getInt();
//            long systemTime = bbuf.getLong();
//
//            int nasIPLen = bbuf.getInt();
//            byte[] nasIPBuf = new byte[nasIPLen];
//            bbuf.get(nasIPBuf);
//
//            int startPortInt = bbuf.getInt();
//            int endPortInt = bbuf.getInt();
//
//            int userNameLen = bbuf.getInt();
//            byte[] userNameBuf = new byte[userNameLen];
//            bbuf.get(userNameBuf);
//
//            int nasPortTypeInt = bbuf.getInt();
//            int nasPortInt = bbuf.getInt();
//            int nasPortIDLen = bbuf.getInt();
//            byte[] nasPortIDBuf = new byte[nasPortIDLen];
//            bbuf.get(nasPortIDBuf);
//            int nasIDLen = bbuf.getInt();
//            byte[] nasIDBuf = new byte[nasIDLen];
//            bbuf.get(nasIDBuf);
//
//            int callintStationLen = bbuf.getInt();
//            byte[] callintStationBuf = new byte[callintStationLen];
//            bbuf.get(callintStationBuf);
//
//            int bsidLen = bbuf.getInt();
//            byte[] bsidBuf = new byte[bsidLen];
//            bbuf.get(bsidBuf);
//
//            int userAddressTypeInt = bbuf.getInt();
//            int userAddressLogLen = bbuf.getInt();
//            byte[] userAddressLogBuf = new byte[userAddressLogLen];
//            bbuf.get(userAddressLogBuf);
//
//            int framedIpv6PrefixInt = bbuf.getInt();
//            int delegatedIpv6PrefixInt = bbuf.getInt();
//            int timestamp = bbuf.getInt();
////                                System.out.println(new String(adBuf));
//
////            System.out.println("userName:" + new String(userNameBuf) + ", online:" + onlineInt + ", ipRaw:" + IPv4Util.bytesToIp(ipRawBuf) + ", nasIP:" + IPv4Util.bytesToIp(nasIPBuf) + ", nasID:" + new String(nasIDBuf)
////                    + ", userAddressType:" + userAddressTypeInt + ", userAddressLog:" + new String(userAddressLogBuf) + ", timestamp:" + timestamp);
//            ByteArrayOutputStream os = new ByteArrayOutputStream();
//            //TLV
//            //Acct-Status-Type 40
//            byte acctStatusType = 40;
////                                byte[] acctStatus = combineTlv(acctStatusType,IPv4Util.intToBytes(onlineInt));
//            combineTlv(os, acctStatusType, IPv4Util.intToBytes(onlineInt));
//
//            //User-Name 1
//            byte userNameType = 1;
////                                byte[] userName = combineTlv(userNameType,userNameBuf);
//            combineTlv(os, userNameType, userNameBuf);
//
//            //Framed-IP-Address 8
//            byte framedIpAddressType = 8;
////                                byte[] framedIpAddress = combineTlv(framedIpAddressType,ipRawBuf);
//            combineTlv(os, framedIpAddressType, ipRawBuf);
//
//            //NAS-Identifier 32
//            byte nasIdentifierType = 32;
////                                byte[] nasIdentifier = combineTlv(nasIdentifierType,nasIDBuf);
//            combineTlv(os, nasIdentifierType, nasIDBuf);
//
//            //NAS-IP-Address 4
//            byte nasIpAddressType = 4;
////                                byte[] nasIpAddress = combineTlv(nasIpAddressType,nasIPBuf);
//            combineTlv(os, nasIpAddressType, nasIPBuf);
//
//            byte vendorSpecificType = 26;
//            byte[] vendorID = IPv4Util.intToBytes(20942);
//
//            //USER-ADDRESS-TYPE 120
//            byte userAddressTypeType = 120;
//            byte[] userAddressType = IPv4Util.intToBytes(userAddressTypeInt);
////                                combineTlv(os,userAddressTypeType,IPv4Util.intToBytes(userAddressTypeInt));
//
//            //USER-ADDRESS-LOG 121
//            byte userAddressLogType = 121;
//            byte[] userAddressLog = userAddressLogBuf;
////                                combineTlv(os,userAddressLogType,userAddressLogBuf);
//
////            byte vendorSpecificLen = (byte) (vendorID.length + 2 + userAddressType.length + 2 + userAddressLog.length);
//            os.write(vendorSpecificType);
//            os.write(2 + vendorID.length + 2 + userAddressType.length);
////            os.write(2 + vendorID.length + 2 + 1);
//            os.write(vendorID);
//            os.write(userAddressTypeType);
//            os.write(userAddressType.length+2);
////            os.write(1+2);
//            os.write(userAddressType);
//
//            os.write(vendorSpecificType);
//            os.write((byte)(2 + vendorID.length + 2 + userAddressLog.length));
//            os.write(vendorID);
//            os.write(userAddressLogType);
//            os.write((byte)(userAddressLog.length+2));
//            os.write(userAddressLog);
//
//            //构造 AAA消息
//            byte[] bodyTVL = os.toByteArray();
//            os.close();
//            UcRawMessage aaamsg = new UcRawMessage();
//            aaamsg.setMessageType(new UcType.UINT1(new byte[]{(byte) 0x81}));
//            aaamsg.setMessageNo(UcType.newUINT4(1));
//            aaamsg.setMessageLength(UcType.newUINT4(bodyTVL.length + UcRawMessage.HEADER_LENGTH));
//            aaamsg.setMessageSequenceNo(UcType.newUINT4(1));
//            aaamsg.setManaul(false);
//            aaamsg.setBody(bodyTVL);
//
////            System.out.println(new String(userNameBuf));
////            System.out.println(userAddressTypeInt);
////            System.out.println(new String(userAddressLog));
//
//            //上线信息放到内存供导出
//            if (onlineInt == 1) {
//                String[] log = new String(userAddressLog).split(";");
//                String aaatd = new String(userNameBuf) + "|" + InetAddress.getByAddress(ipRawBuf).getHostAddress() + "|" + log[2] + ";" + log[3] + "|" + framedIpv6PrefixInt + "|" + delegatedIpv6PrefixInt + "|" + timestamp;
//                AAAMem.aset.add(aaatd);
//            }
//
//
//            return aaamsg;
//        } catch (Exception e) {
//            A3Pub.exception.incrementAndGet();
//            A3Pub.LOGGER.error("", e);
//        }
//        return null;
//    }
//
//    public void combineTlv(ByteArrayOutputStream os, byte type, byte[] value) throws IOException {
//        os.write(type);
//        os.write((byte) (2 + value.length));
//        os.write(value);
//    }

}
